package com.gxgy.app.controller;

import com.gxgy.app.doman.dto.OrderDto;
import com.gxgy.app.doman.po.Order;
import com.gxgy.app.doman.vo.OrderVo;
import com.gxgy.app.service.OrderService;
import com.gxgy.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/app/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /** 下单的方法 */
    @PostMapping("/add")
    public AjaxResult addOrder(@RequestBody OrderDto orderDto){
        boolean res = orderService.downOrder(orderDto);
        return res ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    /** 获取用户全部订单 */
    @GetMapping("/list/{userId}")
    public AjaxResult list(@PathVariable Long userId){
        List<OrderVo> orderListByUserId = orderService.getOrderListByUserId(userId);
        return AjaxResult.success(orderListByUserId);
    }

    /** 获取用户某个状态订单 */
    @GetMapping("/list/{userId}/{status}")
    public AjaxResult listByStatus(@PathVariable Long userId, @PathVariable Long[] status){
        List<OrderVo> list = orderService.getOrderListByUserIdAndStatus(userId, status);
        return AjaxResult.success(list);
    }

    /** 修改订单 */
    @PutMapping
    public AjaxResult listByStatus(@RequestBody Order order){
        order.setUpdateTime(LocalDateTime.now());
        boolean updated = orderService.updateOrder(order);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }
}
